package com.cmict.data.system.api.user;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.util.collection.CollectionUtils;
import com.cmict.data.system.api.message.WsMessageDTO;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import com.cmict.data.system.enums.ApiConstants;
import org.apache.commons.lang3.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@FeignClient(name = ApiConstants.NAME) // TODO awesome：fallbackFactory =
@Api(tags = "RPC 服务 - 管理员用户")
public interface AdminUserApi {

    String PREFIX = ApiConstants.PREFIX + "/user";

    @GetMapping(PREFIX + "/get")
    @ApiOperation("通过用户 ID 查询用户")
    @ApiImplicitParam(name = "id", value = "用户编号", example = "1", dataTypeClass = Long.class)
    CommonResult<AdminUserRespDTO> getUser(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/list-by-ids-join")
    @ApiOperation("通过用户 ID 查询用户们")
    @ApiImplicitParam(name = "ids", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByIdsJoin(@RequestParam("ids") String ids);

    @GetMapping(PREFIX + "/list")
    @ApiOperation("通过用户 ID 查询用户们")
    @ApiImplicitParam(name = "ids", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<List<AdminUserRespDTO>> getUsers(@RequestParam("ids") Collection<Long> ids);


    @GetMapping(PREFIX + "/list-by-dept-id-join")
    @ApiOperation("获得指定部门的用户数组")
    @ApiImplicitParam(name = "deptIds", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByDeptIdsJoin(@RequestParam("deptIds") String deptIds);

    @GetMapping(PREFIX + "/list-by-dept-id")
    @ApiOperation("获得指定部门的用户数组")
    @ApiImplicitParam(name = "deptIds", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByDeptIds(@RequestParam("deptIds") Collection<Long> deptIds);

    @GetMapping(PREFIX + "/list-by-post-id-join")
    @ApiOperation("获得指定岗位的用户数组")
    @ApiImplicitParam(name = "postIds", value = "岗位编号数组", example = "2,3", required = true, allowMultiple = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByPostIdsJoin(@RequestParam("postIds") String postIds);

    @GetMapping(PREFIX + "/list-by-post-id")
    @ApiOperation("获得指定岗位的用户数组")
    @ApiImplicitParam(name = "postIds", value = "岗位编号数组", example = "2,3", required = true, allowMultiple = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByPostIds(@RequestParam("postIds") Collection<Long> postIds);

    /**
     * 获得用户 Map
     *
     * @param ids 用户编号数组
     * @return 用户 Map
     */
    default Map<Long, AdminUserRespDTO> getUserMap(Collection<Long> ids) {
        return CollectionUtils.convertMap(getUsers(ids).getCheckedData(), AdminUserRespDTO::getId);
    }
    default Map<Long, AdminUserRespDTO> getUserMapByIdsJoin(Collection<Long> ids) {
        String idList = StringUtils.join(ids, ",");
        return CollectionUtils.convertMap(getUsersByIdsJoin(idList).getCheckedData(), AdminUserRespDTO::getId);
    }
    default Map<Long, AdminUserRespDTO> getUserMapByDeptIds(Collection<Long> ids) {
        String idList = StringUtils.join(ids, ",");
        return CollectionUtils.convertMap(getUsersByDeptIdsJoin(idList).getCheckedData(), AdminUserRespDTO::getId);
    }
    default Map<Long, AdminUserRespDTO> getUserMapByPostIds(Collection<Long> ids) {
        String idList = StringUtils.join(ids, ",");
        return CollectionUtils.convertMap(getUsersByPostIdsJoin(idList).getCheckedData(), AdminUserRespDTO::getId);
    }
    default Map<Long, AdminUserRespDTO> getUserMapByRoleIds(Collection<Long> ids) {
        String idList = StringUtils.join(ids, ",");
        return CollectionUtils.convertMap(getUsersByRoleIdsJoin(idList).getCheckedData(), AdminUserRespDTO::getId);
    }
    default List<AdminUserRespDTO> getUsersByUserNamesInnerMap(Collection<Long> names) {
        String idList = StringUtils.join(names, ",");
        return getUsersByUserNamesInner(idList).getCheckedData();
    }

    @GetMapping(PREFIX + "/valid")
    @ApiOperation("校验用户们是否有效")
    @ApiImplicitParam(name = "ids", value = "用户编号数组", example = "3,5", required = true)
    CommonResult<Boolean> validUsers(@RequestParam("ids") Set<Long> ids);

    @GetMapping(PREFIX + "/users")
    @ApiOperation("所有用户")
    List<AdminUserRespDTO> getAllUsers();

    @GetMapping(PREFIX + "/list-by-role-id-join")
    @ApiOperation("根据角色获取用户列表")
    @ApiImplicitParam(name = "roleIds", value = "用户编号数组", example = "3,5", required = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByRoleIdsJoin(@RequestParam("roleIds") String roleIds);

    @GetMapping(PREFIX + "/list-by-role-id")
    @ApiOperation("根据角色获取用户列表")
    @ApiImplicitParam(name = "ids", value = "用户编号数组", example = "3,5", required = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByRoleIds(@RequestParam("ids")Collection<Long> roleIds);

    @GetMapping(PREFIX + "/list-by-namesInner")
    @ApiOperation("根据用户名获取用户")
    @ApiImplicitParam(name = "names", value = "用户编号数组", example = "li", required = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByUserNamesInner(@RequestParam("names")  String names);

    @GetMapping(PREFIX + "/list-by-names")
    @ApiOperation("根据用户名获取用户")
    @ApiImplicitParam(name = "names", value = "用户编号数组", example = "li", required = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByUserNames(@RequestParam("names")  Collection<String> names);

    @GetMapping(PREFIX + "/all")
    @ApiOperation("所有部门列表")
    List<AdminUserRespDTO> selectListAll();
    @GetMapping(PREFIX + "/getSessionKeys")
    @ApiOperation("获取sessionKeys")
    CommonResult<Collection<Object>> getSessionKeys();

    @PostMapping(PREFIX + "/distribute")
    @ApiOperation("发送消息")
    void distribute(@RequestBody WsMessageDTO messageDO);

    @GetMapping(PREFIX + "/selectData2")
    @ApiOperation("获取AdminUserDO的class")
    @ApiImplicitParam(name = "sql", value = "sql", example = "li", required = true)
    CommonResult<List<String>> selectData2(@RequestParam("sql") String sql);

    @GetMapping(PREFIX + "/list-by-nickname")
    @ApiOperation("根据用户昵称获取用户")
    @ApiImplicitParam(name = "nickname", value = "用户昵称", example = "li", required = true)
    CommonResult<List<AdminUserRespDTO>> getUsersByNickname(@RequestParam("nickname")  String nickname);
}
